1750D - Count GCD - CodeForces Solution


brute force combinatorics math number theory

Please click on ads to support us..

Python Code:

import sys
from math import gcd
input = lambda : sys.stdin.readline().strip()
md = 998244353

for t in range(int(input())):
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    calc = {}
    for i in range(1,n):
        if arr[i-1]%arr[i] != 0:
            print("0")
            break
        calc[(arr[i-1], arr[i])] = 0
    else:
        factors = []
        
        
        d = 2
        f = arr[0]
        while d*d <= f:
            if f%d == 0:
                factors.append(d)
                while f%d == 0:
                    f //= d
            d += 1
        if f != 1:
            factors.append(f)
        
        for k,v in calc.items():
            left, till = k[0] / k[1], m//k[1]
            left_prime = []
            for i in factors:
                if left%i == 0:
                    left_prime.append(i)
            
            sz = len(left_prime)
            ans = 0
            for mask in range(1<<sz):
                prod = 1
                cnt = 0
                for j in range(sz):
                    if mask & (1<<j):
                        prod *= left_prime[j]
                        cnt += 1
                if cnt%2 == 0:
                    ans += till//prod
                else:
                    ans -= till//prod
            calc[k] = ans
        
        ans = 1
        
        for i in range(1,n):
            ans *= calc[(arr[i-1], arr[i])]
            
            ans %= md
        print(ans)


C++ Code:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpi;
typedef vector<ll> vl;
typedef vector<pair<ll, ll>> vpl;
// typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 1e18 + 2;
const ld pie = 3.141592653589793238462;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define minv(a) *min_element(a.begin(), a.end())
#define maxv(a) *max_element(a.begin(), a.end())
#define acc(a,n) accumulate(a.begin(),a.end(),n)
#define inp(v) for(auto& i: v) cin>>i

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define stp(n) fixed << setprecision(n)

#define fastio                        \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);                    \
  cout.tie(NULL);
#define cases          \
  int test_cases;    \
  cin >> test_cases; \
  while (test_cases--)

bool inc(pair<ll, ll> p1, pair<ll, ll> p2)
{
  if(p1.ff < p2.ff) return true;
  if(p1.ff > p2.ff) return false;
  return p1.ss > p2.ss;
}

int nCr(int n, int r, int mod){
  if(n < r || n < 0 || r < 0) return 0;

  int dp[n+1][r+1];

  for(int i=0; i<=n; i++) for(int j=0; j<=r; j++) dp[i][j] = 0;

  for(int i=0; i<=n; i++) dp[i][0] = 1;

  for(int i=1; i<=n; i++){
    for(int j=1; j<=r; j++){
      dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
      dp[i][j] %= mod;
    }
  }

  return dp[n][r]; 
}

void lgc(vi &lg, int N){
  for(int i=2; i<N; i++) lg[i] = lg[i/2] + 1;
  return;
}

void max_sparse(vector<vector<int>> &st, int N, vi &v){
  for(int i=0; i<v.size(); i++) st[i][0] = v[i];

  for(int j = 1; j < 25; j++){
    for(int i = 0; i + (1 << j) <= N; i++){
      st[i][j] = max(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);
    }
  }
  return;
}

long long binpow(long long a, long long b) {
  if (b == 0) return 1;
  long long res = binpow(a, b / 2);
  if (b % 2) return res * res * a;
  else return res * res;
}

ll phi(ll n) {
  ll result = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0)
        n /= i;
      result -= result / i;
    }
  }
  if (n > 1)
    result -= result / n;
  return result;
}

vl get_factors(ll a){
  vl ans;
  for(ll i=2; i*i<=a; i++){
    if(a%i == 0) ans.pb(i);
    while(a%i == 0) a/=i;
  }
  if(a != 1) ans.pb(a);
  return ans;
}

void solve(){
  
  ll n, m, ans = 1;
  cin>>n>>m;

  vl a(n);
  inp(a);

  map<ll, vl> pf;

  vl fac = get_factors(a[0]);

  for(ll i=1; i<n; i++){
    if(a[i-1]%a[i] != 0){
      cout<<"0\n";
      return;
    }
    ll mx = 0;
    ll left = a[i-1]/a[i];

    vl uf;
    for(auto i:fac) if(left % i == 0) uf.pb(i);
    ll sz = uf.size();
    for(ll k=0; k<(1<<sz); k++){
      ll pro = 1, cnt = 0;
      for(ll j=0; j<sz; j++){
        if(k & (1<<j)){
          pro *= uf[j];
          cnt++;
        }
      }
      if(cnt%2 == 0) mx += (m/a[i]) / pro;
      else mx -= (m/a[i]) / pro;
    }

    ans *= mx;
    ans %= MOD2;
  }

  cout<<ans<<"\n";

  return;
}

int32_t main()
{
  fastio

  // #ifndef ONLINE_JUDGE               
  //   freopen("input.txt", "r", stdin);                                           
  //   freopen("output.txt", "w", stdout);  
  //   freopen("error.txt", "w", stderr);                        
  // #endif 

  // int t;
  // cin>>t;
  // for(int i=1; i<=t; i++){
  //   cout<<"Case #"<<i<<": ";
  //   solve();
  // }

  cases{
    solve();
  }

	// solve();
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees